﻿using Ldt.DataEngine.LogAssembly.Core;
using System;
using System.Collections.Generic;

namespace Ldt.DataEngine.LogHandle
{
    /// <summary>
    /// 日志操作类
    /// </summary>
    public class LogHandle
    {
        /// <summary>
        /// 消息类型
        /// </summary>
        public enum LogHType
        {
            /// <summary>
            /// The error
            /// </summary>
            Error,
            /// <summary>
            /// The information
            /// </summary>
            Info,
            /// <summary>
            /// The debug
            /// </summary>
            Debug,
            /// <summary>
            /// The fatal
            /// </summary>
            Fatal,
            /// <summary>
            /// The trace
            /// </summary>
            Trace,
            /// <summary>
            /// The warn
            /// </summary>
            Warn

        }

        private ILog log;

        private static LogHandle _loghandle;

        /// <summary>
        /// 获取实例化的日志组件
        /// </summary>
        public static LogHandle GetInitLogHandle
        {
            get
            {
                return _loghandle == null ? new LogHandle() : _loghandle;
            }
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        private LogHandle()
        {
            LogLibFactory.AddOrUpdateLayoutRendererValue("systemplatform");
            LogLibFactory.AddOrUpdateLayoutRendererValue("baseDirectory");
            LogLibFactory.AddOrUpdateLayoutRendererValue("identifier");
            LogLibFactory.AddOrUpdateLayoutRendererValue("typename");
            LogLibFactory.AddOrUpdateLayoutRendererValue("customid");
            LogLibFactory.AddOrUpdateLayoutRendererValue("handletypecode");
            LogLibFactory.AddOrUpdateLayoutRendererValue("modulecode");
            log = LogLibFactory.Create();
        }


        /// <summary>
        /// 进行更新NLog数据变量信息
        /// </summary>
        /// <param name="valuePairs">变量集合</param>
        public void UpdateLogVar(IDictionary<string, string> valuePairs)
        {
           // LogLibFactory.UpdateVariable(valuePairs);
            if (valuePairs!=null)
            {
                string logstashtcp = "";
                string logLevel = "";
                if (valuePairs.ContainsKey("varlogstashtcp"))
                {
                    logstashtcp = valuePairs["varlogstashtcp"];
                }
                if (valuePairs.ContainsKey("varloglevel"))
                {
                    if (!string.IsNullOrEmpty(valuePairs["varloglevel"]))
                    {
                        logLevel = valuePairs["varloglevel"];
                    }
                    else
                    {
                        logLevel = "Info";
                    }
                    
                }
                else
                {
                    logLevel = "Info";
                }
                LdtCommonLogger.LoggerExtend.SetLogstashAndLoglevel(logstashtcp,logLevel);
            }
            
        }


        /// <summary>
        /// 写入消息日志
        /// </summary>
        /// <param name="message">消息内容</param>
        public void LogWrite(string message)
        {
            this.LogWrite(message, null);
        }
        /// <summary>
        /// 写入消息日志
        /// </summary>
        /// <param name="message">消息内容</param>
        /// <param name="variables">变量数据</param>
        public void LogWrite(string message, Dictionary<string, string> variables)
        {
            this.LogWrite(LogHType.Info, message, variables);
        }
        /// <summary>
        /// 写入消息日志
        /// </summary>
        /// <param name="logHType">日志类型</param>
        /// <param name="message">日志消息</param>
        public void LogWrite(LogHType logHType, string message)
        {
            this.LogWrite(logHType, message, null);
        }

        /// <summary>
        /// 写入日志信息
        /// </summary>
        /// <param name="logHType">日志类型</param>
        /// <param name="message">消息内容</param>
        /// <param name="variables">变量数据</param>
        public void LogWrite(LogHType logHType, string message, Dictionary<string, string> variables)
        {
            LogType logty = LogType.Info;
            foreach (LogType typ in Enum.GetValues(typeof(LogType)))
            {
                if (((int)logHType) == ((int)typ))
                {
                    logty = typ;
                    break;
                }
            }
            log.WriteLogs(logty, variables,  message);

        }


    }
}
